/*
 *             Copyright (C) 2024 Steve Jrong
 *
 * 	   GitHub Homepage: https://www.github.com/SteveJrong
 *      Gitee Homepage: https://gitee.com/stevejrong1024
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stevejrong.waiter.user.dbsdk.dao;

import org.hibernate.reactive.mutiny.Mutiny.SessionFactory;
import org.hibernate.reactive.mutiny.Mutiny.StatelessSession;
import org.springframework.stereotype.Repository;

import com.stevejrong.waiter.user.dbsdk.model.GuestPO;

import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.converters.uni.UniReactorConverters;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import reactor.core.publisher.Mono;

/**
 * 仓储接口 - 顾客
 *
 * @author Steve Jrong
 * @since 1.0
 */
@Repository("guestDao")
public class IGuestDao {
	
	/**
	 * 获取Session工厂
	 */
	private SessionFactory getSessionFactory() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("example");
        return emf.unwrap(SessionFactory.class);
    }
	
	/**
	 * 获取一个Hibernate的底层会话
	 * 
	 * @return
	 */
	private Uni<StatelessSession> getStatelessSession() {
        return getSessionFactory().openStatelessSession();
    }
	
	public Mono<Void> insert(GuestPO guestPO){
		return this.getSessionFactory()
				.withTransaction((session, transaction) -> session.persist(guestPO))
				.convert()
				.with(UniReactorConverters.toMono()).log();
	}
}